import Mock from 'mockjs'
import { param2Obj } from '@/utils'

const list = []
const count = Mock.mock('@integer(50, 1000)')
for (let i = 0; i < count; i++) {
  list.push(Mock.mock({
    id: i,
    status: '@cword("abc")', // a未审核 b审核中 c已审核
    orderNo: 'POOD' + (('000000'.concat(i)).substr(-6, 6)),
    date: '@date("yyyy-MM-dd")',
    creator: '@cname',
    qty: '@float(10, 1000, 2, 2)',
    unit: '@cword("个米斤")',
    price: '@float(1, 100, 2, 2)',
    amount: '@float(10, 10000, 2, 2)',
    address: '@city(true)'
  }))
}

function sum (prop) {
  var s = 0
  for (let itm of list) {
    s += itm[prop]
  }
  return s.toFixed(2)
}

export default [{
  path: 'srm/purchase',
  type: 'get',
  template: option => {
    const { url } = option
    const query = param2Obj(url)
    return {
      success: true,
      msg: '',
      data: {
        count,
        summary: {
          qty: sum('qty'),
          amount: sum('amount')
        },
        list: list.slice((query.page - 1) * query.limit, query.page * query.limit)
      }
    }
  }
}]
